/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package access_db_design_pattern;

import java.awt.Color;

/**
 *
 * @author tordeepweb
 */
public class DataBaseManager extends javax.swing.JFrame {

    private static final String[] COULEURS = new String[]{
        "#9590FF",
        "#FF0033",
        "#186FDF",
        "#568203",
        "#DEAA2C",
        "#FDBD96",
        "#91283B",
        "#ED0000",
        "#A91101",
        "#A10684",
        "#57D53B"};

    /**
     * Creates new form DataBaseManager
     */
    public DataBaseManager() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelDatabaseManager = new javax.swing.JLabel();
        jLabelCode_marque = new javax.swing.JLabel();
        jTextFieldCode_marque = new javax.swing.JTextField();
        jLabelLibelle_marque = new javax.swing.JLabel();
        jTextFieldLibelle_marque = new javax.swing.JTextField();
        jButtonAjouter_marque = new javax.swing.JButton();
        jButtonSupprimer_marque = new javax.swing.JButton();
        jButtonObtenir_marque = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaEtat = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelDatabaseManager.setFont(new java.awt.Font("Century Gothic", 1, 14)); // NOI18N
        jLabelDatabaseManager.setText("                         DATA BASE MANAGER");

        jLabelCode_marque.setText("code_marque");

        jLabelLibelle_marque.setText("libelle_marque");

        jButtonAjouter_marque.setText("Ajouter Marque");
        jButtonAjouter_marque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAjouter_marqueActionPerformed(evt);
            }
        });

        jButtonSupprimer_marque.setText("Supprimer Marque");
        jButtonSupprimer_marque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSupprimer_marqueActionPerformed(evt);
            }
        });

        jButtonObtenir_marque.setText("Obtenir marque");
        jButtonObtenir_marque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonObtenir_marqueActionPerformed(evt);
            }
        });

        jTextAreaEtat.setColumns(20);
        jTextAreaEtat.setRows(5);
        jScrollPane1.setViewportView(jTextAreaEtat);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(44, 44, 44)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabelLibelle_marque, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                                .addComponent(jLabelCode_marque, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(125, 125, 125)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextFieldCode_marque)
                                .addComponent(jTextFieldLibelle_marque)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(188, 188, 188)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jButtonAjouter_marque, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonSupprimer_marque, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                .addComponent(jButtonObtenir_marque, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(117, 117, 117)
                        .addComponent(jLabelDatabaseManager, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(80, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jLabelDatabaseManager, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelCode_marque, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldCode_marque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelLibelle_marque)
                    .addComponent(jTextFieldLibelle_marque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonAjouter_marque)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonSupprimer_marque)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonObtenir_marque))
                    .addComponent(jScrollPane1))
                .addContainerGap(65, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAjouter_marqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAjouter_marqueActionPerformed
        // TODO add your handling code here:
        Marque uneMarque = new Marque();
        uneMarque.setLibelle_marque(jTextFieldLibelle_marque.getText());
        MarqueManager unMM = new MarqueManager();
        if (unMM.ajouterMarque(uneMarque) == true) {
            String laCouleur = COULEURS[3];
            jTextAreaEtat.setForeground(new Color(Integer.valueOf(laCouleur.substring(1, 3), 16),
            Integer.valueOf(laCouleur.substring(3, 5), 16),
            Integer.valueOf(laCouleur.substring(5, 7), 16)));
            jTextAreaEtat.setText("L'ajout s'est bien effectué");

        } else {
            String laCouleur = COULEURS[5];
            jTextAreaEtat.setForeground(new Color(Integer.valueOf(laCouleur.substring(1, 3), 16),
            Integer.valueOf(laCouleur.substring(3, 5), 16),
            Integer.valueOf(laCouleur.substring(5, 7), 16)));
            jTextAreaEtat.setText("L'ajout a mal fonctionné");
        }

    }//GEN-LAST:event_jButtonAjouter_marqueActionPerformed

    private void jButtonSupprimer_marqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSupprimer_marqueActionPerformed
        // TODO add your handling code here:
        Marque uneMarque = new Marque();
        uneMarque.setCode_marque(Integer.parseInt(jTextFieldCode_marque.getText()));
        MarqueManager unMM = new MarqueManager();
        if (unMM.supprimerMarque(uneMarque) == true) {

            jTextAreaEtat.setForeground(Color.green);
            jTextAreaEtat.setText("La suppression s'est bien effectuée");

        } else {
            
            jTextAreaEtat.setForeground(Color.red);
            jTextAreaEtat.setText("La suppression a mal fonctionnée");
        }
    }//GEN-LAST:event_jButtonSupprimer_marqueActionPerformed

    private void jButtonObtenir_marqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonObtenir_marqueActionPerformed
        // TODO add your handling code here:
        Marque uneMarque;// = new Marque();

        MarqueManager unMM = new MarqueManager();
        uneMarque = unMM.obtenirMarque(Integer.parseInt(jTextFieldCode_marque.getText()));

        if (uneMarque == null) {
            jTextAreaEtat.setForeground(Color.red);
            jTextAreaEtat.setText("Impossible d'obtenir le libelle de ce code.");
        } else {
            
            jTextAreaEtat.setForeground(Color.green);
            jTextAreaEtat.setText(uneMarque.getLibelle_marque());
        }


    }//GEN-LAST:event_jButtonObtenir_marqueActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DataBaseManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DataBaseManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DataBaseManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DataBaseManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DataBaseManager().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAjouter_marque;
    private javax.swing.JButton jButtonObtenir_marque;
    private javax.swing.JButton jButtonSupprimer_marque;
    private javax.swing.JLabel jLabelCode_marque;
    private javax.swing.JLabel jLabelDatabaseManager;
    private javax.swing.JLabel jLabelLibelle_marque;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaEtat;
    private javax.swing.JTextField jTextFieldCode_marque;
    private javax.swing.JTextField jTextFieldLibelle_marque;
    // End of variables declaration//GEN-END:variables
}
